package com.itheima.controller;

import com.itheima.domain.User;
import com.itheima.domain.UserSelective;
import com.itheima.service.UserService;
import com.itheima.util.RequestSessionUtil;
import entity.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Date;


/**
 * Created with IntelliJ IDEA.
 * @Author: auuuu4
 * @Date: 2024/07/09/10:32
 * @Description:
 */
@RestController
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    UserService userService;

    @PostMapping(value = "/login")
    public void login(@RequestParam String email, @RequestParam String password, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            User user = userService.findByEmail(email);
            String msg = "";
            if(user == null || user.getStatus().equals("1")){
                RequestSessionUtil.setAttribute(request,"msg","用户被禁用或信息不符");
                request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
                return;
            }

            if(user.getPassword().equals(password)){
                RequestSessionUtil.setUserInfo(request,user);
                if(user.isAdmin()){
                    request.getRequestDispatcher("/admin/main.jsp").forward(request,response);
                }
                else
                    request.getRequestDispatcher("/admin/index.jsp").forward(request,response);
            }
            else{
                RequestSessionUtil.setAttribute(request,"msg","邮箱或密码错误");
                request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
            }
        }catch (Exception e){
            e.printStackTrace();
            RequestSessionUtil.setAttribute(request,"msg","服务器繁忙，请稍后再试");
            request.getRequestDispatcher("/admin/login.jsp").forward(request,response);
        }

    }
    @RequestMapping(value = "/search")
    public void search(Integer id,String name, Integer pageNum ,Integer pageSize,HttpServletRequest request,HttpServletResponse response){
        try {
            UserSelective selective = new UserSelective(id,name);
            RequestSessionUtil.setAttribute(request,"pageResult",userService.selectUsersSelective(selective));
            request.getRequestDispatcher("/admin/user.jsp").forward(request,response);

        }catch (Exception e){
            e.printStackTrace();

        }

    }
    @GetMapping("/findById")
    public User findById(@RequestParam int id){
        try {
            return userService.findById(id);
        }catch (IllegalArgumentException e){
            e.printStackTrace();
            return null;
        }
    }
    @PostMapping("/addUser")
    public Result addUser(@RequestParam String name, @RequestParam String email, @RequestParam Date hiredate, @RequestParam String password, @RequestParam String role){
        try{
            int i = userService.addUser(new User(null,name,password,email,role,"0",hiredate));
            if(i > 0)
                return Result.success();
            else
                return Result.fail("操作失败");
        }catch (IllegalArgumentException e){
            return Result.fail("传入空值了或用户"+email+"已经存在");
        }catch (Exception e){
            return Result.error();
        }

    }
    @GetMapping("/logout")
    public Result logout(HttpServletRequest request,HttpServletResponse response) throws IOException {
        request.getSession().invalidate();
        response.sendRedirect("/cloudlibrary/admin/login.jsp");
        return Result.success();
    }
    @PostMapping("/editUser")
    public Result editUser(@RequestParam Integer id,@RequestParam String name,
                           @RequestParam String email,@RequestParam Date hiredate,@RequestParam String password,@RequestParam String role,HttpServletRequest request){
        User u = new User(id,name,password,email,role,null,hiredate);
        if(userService.updateUser(u) > 0){
            if(RequestSessionUtil.getUserInfo(request).getEmail().equals(email))
                RequestSessionUtil.setUserInfo(request,u);
            return Result.success();
        }

        else
            return Result.fail();
    }
    @GetMapping("/delUser")
    public Result delUser(@RequestParam Integer id){
        if(userService.updateUser(new User(id,null,null,null,null,"1",null)) > 0)
            return Result.success();
        else
            return Result.fail();
    }
}
